using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 火炬树桩 - 将经过的子弹转换为火焰子弹
/// 使用策略模式和外观模式实现子弹转换
/// </summary>
public class Torchwood : Plant
{
    private TorchwoodData m_TorchwoodData;
    private TorchwoodSkillData m_SkillData;
    private float m_EffectRange = 1.0f;
    private IBulletConverter m_BulletConverter;
    private FireBulletConverterData m_ConverterData;

    public override void Initialize()
    {
        base.Initialize();
        
        // 获取 TorchwoodData
        if (EntityData is TorchwoodData torchwoodData)
        {
            m_TorchwoodData = torchwoodData;
            m_EffectRange = torchwoodData.EffectRange;
        }
        else
        {
            Logger.LogWarning("Torchwood", $"Torchwood 的 EntityData 不是 TorchwoodData 类型");
        }

        // 获取技能数据
        if (AttackComponent != null && AttackComponent.AttackData is TorchwoodSkillData skillData)
        {
            m_SkillData = skillData;
            InitializeConverter();
        }
        else
        {
            Logger.LogWarning("Torchwood", $"Torchwood 的技能数据不是 TorchwoodSkillData 类型");
        }

        // 确保有触发器用于检测子弹
        SetupTrigger();
    }

    /// <summary>
    /// 初始化转换器（策略模式）
    /// </summary>
    private void InitializeConverter()
    {
        if (m_SkillData == null)
        {
            return;
        }

        // 创建转换器
        m_BulletConverter = new FireBulletConverter();

        // 创建转换数据
        m_ConverterData = new FireBulletConverterData
        {
            TargetPrefab = m_SkillData.FireBulletPrefab,
            DamageMultiplier = m_SkillData.DamageMultiplier,
            BonusDamage = m_SkillData.FireDamageBonus,
            BuffsToAdd = m_SkillData.BuffsToAdd ?? new List<BuffData>(),
            EffectsToAdd = m_SkillData.EffectsToAdd ?? new List<InstantEffectData>()
        };

        Logger.Log("Torchwood", $"转换器初始化完成: 伤害倍数={m_ConverterData.DamageMultiplier}, Buff数量={m_ConverterData.BuffsToAdd.Count}");
    }

    /// <summary>
    /// 设置触发器用于检测子弹
    /// </summary>
    private void SetupTrigger()
    {
        var collider = GetComponent<Collider2D>();
        if (collider == null)
        {
            Logger.LogWarning("Torchwood", $"{name} 没有 Collider2D 组件，无法检测子弹");
            return;
        }

        // 确保是触发器
        collider.isTrigger = true;
        
        // 如果是 BoxCollider2D，可以根据影响范围调整大小
        if (collider is BoxCollider2D boxCollider)
        {
            boxCollider.size = new Vector2(m_EffectRange * 2, m_EffectRange * 2);
        }
        else if (collider is CircleCollider2D circleCollider)
        {
            circleCollider.radius = m_EffectRange;
        }
    }

    public override PlantType GetPlantType()
    {
        return PlantType.Torchwood;
    }

    /// <summary>
    /// 检测进入触发器的对象（用于检测子弹）
    /// </summary>
    private void OnTriggerEnter2D(Collider2D other)
    {
        // 检测是否是子弹
        var bullet = other.GetComponent<Bullet>();
        if (bullet == null)
        {
            return;
        }

        // 检查是否应该转换子弹
        if (m_TorchwoodData == null || !m_TorchwoodData.ConvertToFirePea)
        {
            return;
        }

        // 检查转换器是否可用
        if (m_BulletConverter == null || m_ConverterData == null)
        {
            Logger.LogWarning("Torchwood", "转换器未初始化，无法转换子弹");
            return;
        }

        // 使用转换器转换子弹（策略模式）
        if (m_BulletConverter.CanConvert(bullet))
        {
            m_BulletConverter.Convert(bullet, m_ConverterData);
            Logger.Log("Torchwood", $"将子弹 {bullet.name} 转换为火焰子弹");
        }
    }
}
